//
//  CATest1ViewController.m
//  qiche
//
//  Created by Seimei on 12-11-7.
//  Copyright (c) 2012年 iCar. All rights reserved.
//

#import "CATest1ViewController.h"


@interface CATest1ViewController ()
{
    NSMutableArray *_titleArray;
    UILabel *_tableHeaderLabel;
    NSMutableArray *_titleValue;

}

@end

@implementation CATest1ViewController
@synthesize dash = _dash,rest = _rest;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //self.tabBarItem.image = [UIImage imageNamed:@"first"];
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.title = @"100公里每小时加速测试";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem  alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(pop)];
    
    
    UIImageView *im = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"0-100km.png"]];
    [_dash addSubview:im];
    [_dash sendSubviewToBack:im];
    [im release];
    _dash.minNumber = 0;
    _dash.maxNumber = 250;
    _dash.value = 0;

    //self.navigationItem.title = @"xxxxxx";
    //self.navigationItem.backBarButtonItem = [UINavigationItem ]
    // Do any additional setup after loading the view from its nib.
    
    //NSTimer *t2 = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(update) userInfo:nil repeats:NO];
    if (!_titleArray) {
        _titleArray = [[NSMutableArray alloc] initWithObjects:@"行驶时间:",@"行驶路程:",@"最大马力:",@"最大扭力:",@"品牌:",@"车型:",@"年份:",@"当前里程:", nil];
    }
    
    if (!_titleValue) {
        _titleValue = [[NSMutableArray alloc] initWithObjects:@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0", nil];
    }


    [self performSelector:@selector(update) withObject:nil afterDelay:1.0];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_dash release];
    [_rest release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setDash:nil];
    [self setResult:nil];
    [self setRest:nil];
    [super viewDidUnload];
}

- (void) update {
    
    if ([SensorsService vehicleSpeed] > 10) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"必须在速度为0才能测试" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        return ;
   }
    
    while ([SensorsService vehicleSpeed] < 100) {
        [SensorsService testing:0.05];
        _dash.value = [SensorsService vehicleSpeed];
        
        usleep(5000);
    }
    
    if([SensorsService vehicleSpeed] >= 100) {
        [_titleValue replaceObjectAtIndex:0 withObject:[NSString stringWithFormat:@"%f",[SensorsService testTotalTime]]];
        [_titleValue replaceObjectAtIndex:1 withObject:[NSString stringWithFormat:@"%f",[SensorsService travelTotalDistance]]];
    }
}

- (void) pop {
    
    //[self dismissModalViewControllerAnimated:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark- UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)activeTableView numberOfRowsInSection:(NSInteger)section
{
    if (_titleArray) {
        return [_titleArray count];
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *tagStr = @"test1Cell";
    UITableViewCell *cell=[aTableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%@%d",tagStr,indexPath.row]];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%@%d",tagStr,indexPath.row]] autorelease];
    }
    if([_titleValue objectAtIndex:indexPath.row] != @"0") {
        cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",[_titleArray objectAtIndex:indexPath.row] ,[_titleValue objectAtIndex:indexPath.row]];
    }
    else {
        cell.textLabel.text = [_titleArray objectAtIndex:indexPath.row];
    }
    
    cell.textLabel.textColor = [UIColor brownColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14.0f];
    return cell;
}

#pragma mark- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 35.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (!_tableHeaderLabel) {
        _tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,self.rest.frame.size.width, 30)];
        _tableHeaderLabel.backgroundColor=[UIColor redColor];
        _tableHeaderLabel.text = @"测试结果:";
        _tableHeaderLabel.textColor = [UIColor whiteColor];
        _tableHeaderLabel.font = [UIFont systemFontOfSize:14.0f];
    }
    return _tableHeaderLabel;
}

#pragma mark-Alert
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self pop];
}
@end
